package com.qzsoft.zhjd.laboratory.modules.laboratorymechanics.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.zhjd.collect.modules.cement.entity.NCementJwDataEntity;
import com.qzsoft.zhjd.collect.modules.cement.service.NCementJwDataService;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.laboratory.modules.asphaltPenetrometer.entity.AsphaltPenetrometerResEntity;
import com.qzsoft.zhjd.laboratory.modules.asphaltPenetrometer.service.AsphaltPenetrometerResService;
import com.qzsoft.zhjd.laboratory.modules.laboratoryMechanicsWGroup.entity.MechanicsWDetailEntity;
import com.qzsoft.zhjd.laboratory.modules.laboratoryMechanicsWGroup.entity.MechanicsWFyDetailEntity;
import com.qzsoft.zhjd.laboratory.modules.laboratoryMechanicsWGroup.entity.MechanicsWGroupEntity;
import com.qzsoft.zhjd.laboratory.modules.laboratoryMechanicsWGroup.mapper.MechanicsWGroupMapper;
import com.qzsoft.zhjd.laboratory.modules.laboratoryMechanicsWGroup.service.MechanicsWDetailService;
import com.qzsoft.zhjd.laboratory.modules.laboratoryMechanicsWGroup.service.MechanicsWFyDetailService;
import com.qzsoft.zhjd.laboratory.modules.laboratoryMechanicsWGroup.service.MechanicsWGroupService;
import com.qzsoft.zhjd.laboratory.modules.laboratoryMechanicsYGroup.entity.MechanicsYDetailEntity;
import com.qzsoft.zhjd.laboratory.modules.laboratoryMechanicsYGroup.entity.MechanicsYGroupEntity;
import com.qzsoft.zhjd.laboratory.modules.laboratoryMechanicsYGroup.mapper.MechanicsYGroupMapper;
import com.qzsoft.zhjd.laboratory.modules.laboratoryMechanicsYGroup.service.MechanicsYDetailService;
import com.qzsoft.zhjd.laboratory.modules.laboratoryMechanicsYGroup.service.MechanicsYGroupService;
import com.qzsoft.zhjd.laboratory.modules.laboratorymechanics.dto.LaboratoryMechanicsPressMachineDto;
import com.qzsoft.zhjd.laboratory.modules.laboratorymechanics.entity.LaboratoryMechanicsAllPowerfulMachineDetailEntity;
import com.qzsoft.zhjd.laboratory.modules.laboratorymechanics.entity.LaboratoryMechanicsAllPowerfulMachineEntity;
import com.qzsoft.zhjd.laboratory.modules.laboratorymechanics.entity.LaboratoryMechanicsPressMachineDetailEntity;
import com.qzsoft.zhjd.laboratory.modules.laboratorymechanics.entity.LaboratoryMechanicsPressMachineEntity;
import com.qzsoft.zhjd.laboratory.modules.laboratorymechanics.mapper.LaboratoryMechanicsPressMachineMapper;
import com.qzsoft.zhjd.laboratory.modules.laboratorymechanics.service.LaboratoryMechanicsAllPowerfulMachineDetailService;
import com.qzsoft.zhjd.laboratory.modules.laboratorymechanics.service.LaboratoryMechanicsAllPowerfulMachineService;
import com.qzsoft.zhjd.laboratory.modules.laboratorymechanics.service.LaboratoryMechanicsPressMachineDetailService;
import com.qzsoft.zhjd.laboratory.modules.laboratorymechanics.service.LaboratoryMechanicsPressMachineService;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * @ClassName LaboratoryMechanicsPressMachineServiceImpl
 * @Description 压力机业务接口实现
 * @author cuiwenlong
 * @since
 */
@Service
public class LaboratoryMechanicsPressMachineServiceImpl extends ServiceImpl<LaboratoryMechanicsPressMachineMapper, LaboratoryMechanicsPressMachineEntity>
					implements LaboratoryMechanicsPressMachineService {
	@Autowired
    private BaseServiceComponent baseServiceComponent;
	@Autowired
	private MechanicsWGroupMapper mechanicsWGroupMapper;

    @Autowired
    private MechanicsYGroupMapper mechanicsYGroupMapper;
	/**
	 * 压力机详情
	 */
	@Autowired
	private LaboratoryMechanicsPressMachineDetailService detailService;
	/**
	 * mapper层
	 */
	@Autowired
	private LaboratoryMechanicsPressMachineMapper mapper;
	/**
	 * 万能机基础信息
	 */
	@Autowired
	private LaboratoryMechanicsAllPowerfulMachineService  powerfulMachineService;
	/**
	 * 万能机详情
	 */
	@Autowired
	private LaboratoryMechanicsAllPowerfulMachineDetailService powerfulMachineDetailService;

	@Autowired
	private AsphaltPenetrometerResService asphaltPenetrometerResService;
	/**
	 * 水稳拌合站数据
	 */
	@Autowired
	private NCementJwDataService nCementJwDataService;

	private final static String DATE_NULL = "1899-12-30 00:00:00.000000";

	@Override
	@Transactional
	public Map<String, Object> Upload(JSONObject json) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(json.get("Group")!=null&&json.getJSONObject("Group").getString("_TestID_")!=null) {
			//获取上传信息
			JSONObject group= json.getJSONObject("Group");
			String testId=group.getString("_TestID_");
			try {
			//注：不同的软件的前缀是不一样的。
			//SuperTest的前缀是 ST_
			//TestMaster的前缀是 TM_
			String deviceType=testId.substring(0, 3);
			if ("ST_".equals(deviceType)) {
				//获取上传试样主表信息
				LaboratoryMechanicsPressMachineEntity pressEntity = new LaboratoryMechanicsPressMachineEntity();
				pressEntity.setSampleKey(group.getString("_Key_"));
				pressEntity.setLingQi(group.getString("_LingQi_"));
				pressEntity.setDeviceId(group.getString("_DeviceID_"));
				pressEntity.setTestId(group.getString("_TestID_"));
				pressEntity.setTester(group.getString("_Tester_"));
				if(group.getString("QDDJ")!=null) {
				pressEntity.setQddj(group.getString("QDDJ"));
				}
				if(group.getString("Size")!=null) {
				pressEntity.setSize(group.getString("Size"));
				}
				if(group.getString("XS")!=null) {
				pressEntity.setXs(group.getString("XS"));
				}
				if(group.getString("YXLZ")!=null) {
				pressEntity.setYxlz(group.getString("YXLZ"));
				}
				if(group.getString("YXQD")!=null) {
				pressEntity.setYxqd(group.getString("YXQD"));
				}
				if(group.getString("PDJG")!=null) {
				pressEntity.setPdjg(group.getString("PDJG"));
				}
				//其他字段
				if(group.getString("Shape")!=null) {
					pressEntity.setShape(group.getString("Shape"));
				}
				if (group.getString("Length")!=null) {
					pressEntity.setLength(group.getString("Length"));
				}
				if (group.getString("Width")!=null) {
					pressEntity.setWidth(group.getString("Width"));
				}
				if (group.getString("Height")!=null) {
					pressEntity.setHeight(group.getString("Height"));
				}
				if (group.getString("Diameter")!=null) {
					pressEntity.setDiameter(group.getString("Diameter"));
				}
				if (group.getString("Span")!=null) {
					pressEntity.setSpan(group.getString("Span"));
				}
				if (group.getString("Sample")!=null) {
					pressEntity.setSample(group.getString("Sample"));
				}
				if (group.getString("PingZhong")!=null) {
					pressEntity.setPinZhong(group.getString("PingZhong"));
				}
				if (group.getString("K")!=null) {
					pressEntity.setK(group.getString("PingZhong"));
				}
				//存储试样主表信息
				this.save(pressEntity);
				//获取试样详细信息
				JSONArray detailArray= group.getJSONArray("Detail");
				List<LaboratoryMechanicsPressMachineDetailEntity> detailList = new ArrayList<LaboratoryMechanicsPressMachineDetailEntity>();
				for (Object object : detailArray) {
					JSONObject detailObj = (JSONObject) JSON.toJSON(object);
					LaboratoryMechanicsPressMachineDetailEntity entity = new LaboratoryMechanicsPressMachineDetailEntity();
					entity.setSampleId(pressEntity.getId());
					entity.setSampleKey(group.getString("_Key_"));
					entity.setDetailKey(detailObj.getString("_Key_"));
					entity.setTime(detailObj.getString("_Time_"));
					entity.setLoad(detailObj.getString("Load"));
					entity.setStress(detailObj.getString("Stress"));
					entity.setDatas(detailObj.getString("_Datas_"));
					detailList.add(entity);
				}
				//试样详细信息批量插入数据库
				detailService.saveDetailData(detailList);
			}else if("TM_".equals(deviceType)) {
				//获取上传万能机试样主表信息
				LaboratoryMechanicsAllPowerfulMachineEntity powerfulEntity = new LaboratoryMechanicsAllPowerfulMachineEntity();
				powerfulEntity.setSampleKey(group.getString("_Key_"));
				powerfulEntity.setDeviceId(group.getString("_DeviceID_"));
				powerfulEntity.setTestId(group.getString("_TestID_"));
				powerfulEntity.setTester(group.getString("_Tester_"));
				if(group.getString("GCZJ")!=null) {
					powerfulEntity.setGczj(group.getString("GCZJ"));
				}
				if(group.getString("Area")!=null) {
				powerfulEntity.setArea(group.getString("Area"));
				}
				if(group.getString("So")!=null) {
				powerfulEntity.setSo(group.getString("So"));
				}
				if(group.getString("Shape")!=null) {
				powerfulEntity.setShape(group.getString("Shape"));
				}
				if(group.getString("Size")!=null) {
				powerfulEntity.setSize(group.getString("Size"));
				}
				if(group.getString("Span")!=null) {
				powerfulEntity.setSpan(group.getString("Span"));
				}
				if(group.getString("Length")!=null) {
				powerfulEntity.setLength(group.getString("Length"));
				}
				if(group.getString("Width")!=null) {
				powerfulEntity.setWidth(group.getString("Width"));
				}
				if(group.getString("Height")!=null) {
				powerfulEntity.setHeight(group.getString("Height"));
				}
				if(group.getString("do")!=null) {
				powerfulEntity.setDos(group.getString("do"));
				}
				if(group.getString("ZJ")!=null) {
				powerfulEntity.setZj(group.getString("ZJ"));
				}
				powerfulEntity.setCreateDate(new Date());
				//存储试样主表信息
				powerfulMachineService.savePowerfulData(powerfulEntity);
				//获取试样详细信息
				JSONArray detailArray= group.getJSONArray("Detail");
				List<LaboratoryMechanicsAllPowerfulMachineDetailEntity> detailList = new ArrayList<LaboratoryMechanicsAllPowerfulMachineDetailEntity>();
				for (Object object : detailArray) {
					JSONObject detailObj = (JSONObject) JSON.toJSON(object);
					LaboratoryMechanicsAllPowerfulMachineDetailEntity entity = new LaboratoryMechanicsAllPowerfulMachineDetailEntity();
					entity.setSampleId(powerfulEntity.getId());
					entity.setSampleKey(group.getString("_Key_"));
					entity.setDetailKey(detailObj.getString("_Key_"));
					entity.setTime(detailObj.getString("_Time_"));
					entity.setDatas(detailObj.getString("_Datas_"));
					entity.setLo(detailObj.getString("Lo"));
					entity.setFm(detailObj.getString("Fm"));
					entity.setRm(detailObj.getString("Rm"));
					entity.setFeH(detailObj.getString("FeH"));
					entity.setReH(detailObj.getString("ReH"));
					entity.setFeL(detailObj.getString("FeL"));
					entity.setReL(detailObj.getString("ReL"));
					entity.setLu(detailObj.getString("Lu"));
					entity.setA(detailObj.getString("a"));
					entity.setLc(detailObj.getString("lc"));
					entity.setStdRe(detailObj.getString("StdRe"));
					entity.setSu(detailObj.getString("Su"));
					entity.setZ(detailObj.getString("Z"));
					entity.setFp(detailObj.getString("Fp"));
					entity.setRp(detailObj.getString("Rp"));
					entity.setFt(detailObj.getString("Ft"));
					entity.setRt(detailObj.getString("Rt"));
					entity.setE(detailObj.getString("E"));
					entity.setDlm(detailObj.getString("DLm"));
					entity.setAgt(detailObj.getString("Agt"));
					entity.setAt(detailObj.getString("At"));
					entity.setLm(detailObj.getString("Lm"));
					entity.setAg(detailObj.getString("Ag"));
					entity.setDltz(detailObj.getString("DLTZ"));
					entity.setDlxz(detailObj.getString("DLXZ"));
					entity.setDlwz(detailObj.getString("DLWZ"));
					entity.setDatas(detailObj.getString("_Datas_"));
					detailList.add(entity);
				}
				//试样详细信息批量插入数据库
				powerfulMachineDetailService.saveDetailData(detailList);
			}else {
				resultMap.put("result",0);
				resultMap.put("message", "失败原因：_TestID_传参格式有误");
				return resultMap;
			}
			} catch (Exception e) {
				e.printStackTrace();
				resultMap.put("result",0);
				resultMap.put("message", "失败原因："+e.getMessage());
				return resultMap;
			}
			resultMap.put("result",1);
			resultMap.put("message", "上传数据成功");
			return resultMap;
		 }
		//失败
		resultMap.put("result",0);
		resultMap.put("message", "失败原因：上传数据格式异常");
		return resultMap;
	}
	@Override
	public IPage<LaboratoryMechanicsPressMachineDto> page(Map<String, Object> params) {
		IPage<LaboratoryMechanicsPressMachineDto> page = baseServiceComponent.getPage(params, LaboratoryMechanicsPressMachineDto.class);
		IPage<LaboratoryMechanicsPressMachineDto> resultIPage = mapper.selectPage(page, params);

		return resultIPage;
	}

	@Autowired
	private MechanicsWGroupService groupService;
	@Autowired
	private MechanicsWDetailService wdetailService;

	/**
	 * 接收万能机数据库传入数据
	 */
	@Override
	@Transactional
	public Integer saveDataBaseUpload(JSONObject json) {
		MechanicsWGroupEntity group = new MechanicsWGroupEntity();
		//拼装万能机试验数据
		group.setArea(json.getFloat("area"));
		group.setGczj(json.getFloat("gCZJ"));
		group.setLo(json.getFloat("lo"));
		group.setSylx(json.getString("sYLX"));
		//group.setId(json.getInteger("iNDEX_ID"));
		group.setSysj(json.getDate("sYSJ"));
		group.setTester(json.getString("tester"));
		group.setSysl(json.getInteger("sYSL"));
		group.setType(json.getString("type"));
		group.setMachine(json.getString("machine"));
		group.setSybh(json.getString("sYBH"));
		group.setSourceId(json.getInteger("iD"));
		group.setTag(json.getInteger("tag"));
		group.setCreateDate(new Date());
		groupService.save(group);
		//获取试样详细信息
		JSONArray detailArray= json.getJSONArray("detailList");
		List<MechanicsWDetailEntity> detailList = new ArrayList<MechanicsWDetailEntity>();
		for (Object object : detailArray) {
			JSONObject detailObj = (JSONObject) JSON.toJSON(object);
			MechanicsWDetailEntity detail = new MechanicsWDetailEntity();
			detail.setA(detailObj.getFloat("a"));
			detail.setAgt(detailObj.getFloat("agt"));
			detail.setArea(detailObj.getFloat("area"));
			detail.setAt(detailObj.getFloat("at"));
			detail.setFeh(detailObj.getFloat("feH"));
			detail.setFel(detailObj.getFloat("feL"));
			detail.setFm(detailObj.getFloat("fm"));
			detail.setFp(detailObj.getFloat("fp"));
			detail.setGczj(detailObj.getFloat("gCZJ"));
			detail.setGroupId(group.getId());
			//detail.setGroupId(detailObj.getInteger("gROUP_INDEX_ID"));
			//detail.setId(detailObj.getInteger("iNDEX_ID"));
			detail.setLe(detailObj.getFloat("le"));
			detail.setLo(detailObj.getFloat("lo"));
			detail.setLu(detailObj.getFloat("lu"));
			detail.setReh(detailObj.getFloat("reH"));
			detail.setRel(detailObj.getFloat("reL"));
			detail.setRm(detailObj.getFloat("rm"));
			detail.setRp(detailObj.getFloat("rp"));
			detail.setTestbegin(detailObj.getDate("testBegin"));
			detail.setTestend(detailObj.getDate("testEnd"));
			detail.setType(detailObj.getString("type"));
			detail.setXh(detailObj.getInteger("xH"));
			detail.setYsgcsj(detailObj.getString("ySGCSJ"));
			detail.setDlwz(detailObj.getString("dLWZ"));
			detail.setDlxz(detailObj.getString("dLXZ"));
			detailList.add(detail);
		}
		wdetailService.saveBatch(detailList);
		try{
			wGroupDataSend(group.getId());
		} catch (Exception e){
			e.printStackTrace();
		}
		return json.getInteger("iNDEX_ID");
	}

	/**
	 * 风行软件万能机设备数据上传，泰新三标300/600，齐临
	 *  String testType;// 试验类型代码
	 * 【钢筋原材=GJA,钢筋焊接=2,普通混凝土抗压=61, 喷射混凝土抗压=611, 水泥胶砂抗压=62, 净浆抗压=69, 普通混凝土抗折=81, 水泥胶砂抗折=82,净浆抗折=87】
	 * (联网公司可自行定制，且知会我方)
	 */
	@Override
	@Transactional
	public Integer savefxDataBaseUpload(JSONObject jsonObject) {
		MechanicsWGroupEntity group = new MechanicsWGroupEntity();
		JSONObject json = jsonObject.getJSONObject("universal");
		Date date = new Date();
		String deviceNum = json.getString("deviceNum");
		//拼装万能机试验数据
		group.setProjectName(json.getString("projectName"));
		if(StrUtil.isNotBlank(json.getString("result"))){
			group.setEligible(convertResult(json.getString("result")));
		}
//		group.setGczj(json.getFloat("gCZJ"));
		group.setLo(json.getFloat("lo"));
		group.setSylx(json.getString("testType"));
		//group.setId(json.getInteger("iNDEX_ID"));
		group.setSysj(json.getDate("testDate"));
		group.setTester(json.getString("operators"));
		group.setSysl(json.getInteger("sampleCount"));
		group.setType(deviceNum);
		group.setMachine(deviceNum);
		group.setSybh(json.getString("testNo"));
//		group.setSourceId(json.getInteger("iD"));
		group.setTag(convertTag(json.getString("endFlag")));
		group.setCreateDate(date);
		groupService.save(group);
		//获取试样详细信息
		JSONArray detailArray= jsonObject.getJSONArray("dataList");
		List<MechanicsWDetailEntity> detailList = new ArrayList<>();
		for (Object object : detailArray) {
			JSONObject detailObj = (JSONObject) JSON.toJSON(object);
			MechanicsWDetailEntity detail = new MechanicsWDetailEntity();
			detail.setA(detailObj.getFloat("elongation"));
			detail.setAgt(detailObj.getFloat("Agt"));
			detail.setArea(detailObj.getFloat("area"));
//			detail.setAt(detailObj.getFloat("at"));
			detail.setFeh(detailObj.getFloat("feH"));
			detail.setFel(detailObj.getFloat("feL"));
			detail.setFm(detailObj.getFloat("loadValue"));
			detail.setFp(detailObj.getFloat("fpLoad"));
			detail.setGczj(detailObj.getFloat("diameter"));
			detail.setGroupId(group.getId());
			//detail.setGroupId(detailObj.getInteger("gROUP_INDEX_ID"));
			//detail.setId(detailObj.getInteger("iNDEX_ID"));
//			detail.setLe(detailObj.getFloat("le"));
			detail.setLo(detailObj.getFloat("gauge"));
			detail.setLu(detailObj.getFloat("finalLength"));
			detail.setReh(detailObj.getFloat("yieldH"));
			detail.setRel(detailObj.getFloat("yieldL"));
			detail.setRm(detailObj.getFloat("strengthValue"));
//			detail.setRp(detailObj.getFloat("rp"));
			detail.setTestbegin(detailObj.getDate("testBtime"));
			detail.setTestend(detailObj.getDate("testEtime"));
			detail.setType(deviceNum);
			detail.setXh(detailObj.getInteger("sampleNo"));
			detail.setYsgcsj(detailObj.getString("curveData"));
			detail.setDlwz(detailObj.getString("breakPlace"));
			detail.setDlxz(detailObj.getString("breakProp"));
			detail.setUpdateTime(date);
			detailList.add(detail);
		}
		wdetailService.saveBatch(detailList);
		try{
			wGroupDataSend(group.getId());
		} catch (Exception e){
			e.printStackTrace();
		}
		return group.getId();
	}


	@Override
	@Transactional
	public Integer savefxYljDataBaseUpload(JSONObject jsonObject) {
		//拼装压力机试验数据
		MechanicsYGroupEntity group = new MechanicsYGroupEntity();
		JSONObject json = jsonObject.getJSONObject("universal");
		String deviceNum = json.getString("deviceNum");

		//group.setProjectName(json.getString("projectName"));
		//if(StrUtil.isNotBlank(json.getString("result"))){
		//	group.setPdjg(convertResult(json.getString("result")));
		//}
		group.setPdjg(json.getString("result"));
		//group.setShape(json.getString("shape"));
		group.setLq(json.getString("age"));
		group.setSylx(json.getString("testType"));
		group.setSysj(json.getDate("testDate"));
		group.setTester(json.getString("operators"));
		group.setPingzhong(json.getString("sampleType"));
		group.setSysl(json.getInteger("sampleCount"));
		group.setYxqd(json.getString("validStrength"));
		group.setType(deviceNum);
		group.setQddj(json.getString("strength"));
		//group.setSize(json.getString("size"));
		group.setYxlz(json.getFloat("validValue"));
		group.setMachine(deviceNum);
		group.setSybh(json.getString("testNo"));
//		group.setTag(json.getInteger("endFlag"));
		//group.setSourceId(json.getInteger("number"));
		//group.setXs(json.getFloat("xS"));
		group.setMtype("1");
		group.setCreateDate(new Date());
		ygroupService.save(group);
		//获取试样详细信息
		JSONArray detailArray= jsonObject.getJSONArray("dataList");
		List<MechanicsYDetailEntity> detailList = new ArrayList<MechanicsYDetailEntity>();
		for (Object object : detailArray) {
			JSONObject detailObj = (JSONObject) JSON.toJSON(object);
			MechanicsYDetailEntity detail = new MechanicsYDetailEntity();
			//拼装压力机试验详情数据
			detail.setGroupId(group.getId());
			detail.setLoad(detailObj.getFloat("loadValue"));
			//detail.setPingzhong(detailObj.getString("pingZhong"));
			detail.setQddj(detailObj.getString("RmReL"));
			detail.setShape(detailObj.getInteger("shape"));
			detail.setSize(detailObj.getString("side"));
			detail.setStress(detailObj.getFloat("strengthValue"));
			detail.setTestbegin(detailObj.getDate("testBtime"));
			detail.setTestend(detailObj.getDate("testEtime"));
			detail.setType(deviceNum);
			detail.setXh(detailObj.getInteger("sampleNo"));
			detail.setXs(detailObj.getFloat("coefficient"));
			detail.setYsgcsj(detailObj.getString("curveData"));
			detailList.add(detail);
		}
		ydetailService.saveBatch(detailList);
		try{
			yGroupDataSend(group.getId());
		} catch (Exception e){
			e.printStackTrace();
		}
		return 1;
	}




	/**
	 * 无锡建仪抗压抗折机，水泥抗压jy-test
	 * @param json
	 * @return
	 */
	@Override
	@Transactional
	public Integer saveDataBaseUploadWXJY(JSONObject json) {
		//拼装压力机试验数据
		MechanicsYGroupEntity group = new MechanicsYGroupEntity();
		String pzdj = json.getString("pzdj");
//		group.setShape(json.getString("shape"));
		group.setLq(json.getString("lq"));
		group.setSylx(json.getString("sylx"));
		group.setSysj(parseDateFlexible(json.getString("pxrq")));
		group.setPingzhong(pzdj);
		group.setSysl(6);
		group.setYxqd(json.getString("pxyqjz"));
		group.setType(json.getString("type"));
//		group.setQddj(json.getString("qDDJ"));
//		group.setSize(json.getString("size"));
		String pxlzjz = json.getString("pxlzjz");
		group.setYxlz(StrUtil.isEmpty(pxlzjz) || pxlzjz.contains("*") || pxlzjz.contains("无效") ? null : json.getFloat("pxlzjz"));
//		group.setMachine(json.getString("machine"));
		group.setSybh(json.getString("sybh"));
//		group.setTag(json.getInteger("tag"));
		group.setSourceId(json.getInteger("autoid"));
//		group.setXs(json.getFloat("xS"));
		group.setMtype(json.getString("mtype"));
		group.setCreateDate(new Date());
		ygroupService.save(group);
		//获取试样详细信息
		List<MechanicsYDetailEntity> detailList = new ArrayList<>();
		//每组数据6条子数据；
		for (int i = 1; i < 7; i++) {
			MechanicsYDetailEntity detail = new MechanicsYDetailEntity();
			//拼装压力机试验详情数据
			detail.setGroupId(group.getId());
			//如果是带星*的异常数据，就不插入；
			if (json.getString("pxyq" + i).contains("*") || json.getString("pxlz" + i).contains("*")) {
				continue;
			}
			detail.setLoad(json.getFloat("pxlz" + i));
			detail.setPingzhong(pzdj);
//			detail.setQddj(detailObj.getString("qDDJ"));
//			detail.setShape(detailObj.getInteger("shape"));
//			detail.setSize(detailObj.getString("size"));
			detail.setStress(json.getFloat("pxyq" + i));
			detail.setTestbegin(parseDateFlexible(json.getString("pxrq")));
			detail.setTestend(parseDateFlexible(json.getString("pxrq")));
			detail.setType(json.getString("type"));
//			detail.setXh(detailObj.getInteger("xH"));
//			detail.setXs(detailObj.getFloat("xS"));
			detail.setYsgcsj(json.getString("qx" + i));
			detailList.add(detail);
		}
		ydetailService.saveBatch(detailList);
		try{
			yGroupDataSend(group.getId());
		} catch (Exception e){
			e.printStackTrace();
		}
		return json.getInteger("autoid");
	}

	/**
	 * 无锡建仪抗压抗折机，水泥抗折
	 * @param json
	 * @return
	 */
	@Override
	@Transactional
	public Integer saveDataBaseUploadWXJYZ(JSONObject json) {
		//拼装压力机试验数据
		MechanicsYGroupEntity group = new MechanicsYGroupEntity();
		String pzdj = json.getString("pzdj");
		group.setLq(json.getString("lq"));
		group.setSylx(json.getString("sylx"));
		group.setSysj(parseDateFlexible(json.getString("pxrq")));
		group.setPingzhong(pzdj);
		group.setSysl(3);
		group.setYxqd(json.getString("pxyqjz"));
		group.setType(json.getString("type"));
		String pxlzjz = json.getString("pxlzjz");
		group.setYxlz(StrUtil.isEmpty(pxlzjz) || pxlzjz.contains("*") || pxlzjz.contains("无效") ? null : json.getFloat("pxlzjz"));
		group.setSybh(json.getString("sybh"));
		group.setSourceId(json.getInteger("autoid"));
		group.setMtype(json.getString("mtype"));
		group.setCreateDate(new Date());
		ygroupService.save(group);
		//获取试样详细信息
		List<MechanicsYDetailEntity> detailList = new ArrayList<>();
		//每组数据3条子数据；
		for (int i = 1; i < 4; i++) {
			MechanicsYDetailEntity detail = new MechanicsYDetailEntity();
			//拼装压力机试验详情数据
			detail.setGroupId(group.getId());
			//如果是带星*的异常数据，就不插入；
			if (json.getString("pxyq" + i).contains("*") || json.getString("pxlz" + i).contains("*")) {
				continue;
			}
			detail.setLoad(json.getFloat("pxlz" + i));
			detail.setPingzhong(pzdj);
			detail.setStress(json.getFloat("pxyq" + i));
			detail.setTestbegin(parseDateFlexible(json.getString("pxrq")));
			detail.setTestend(parseDateFlexible(json.getString("pxrq")));
			detail.setType(json.getString("type"));
			detail.setYsgcsj(json.getString("qx" + i));
			detailList.add(detail);
		}
		ydetailService.saveBatch(detailList);
		try{
			yGroupDataSend(group.getId());
		} catch (Exception e){
			e.printStackTrace();
		}
		return json.getInteger("autoid");
	}

	/**
	 * 无锡建仪， 压力机数据接收
	 * @param json
	 * @return
	 */
	@Override
	@Transactional
	public Integer saveDataBaseUploadWXJYYLJ(JSONObject json) {
		//拼装压力机试验数据
		MechanicsYGroupEntity group = new MechanicsYGroupEntity();
//		String pzdj = json.getString("pzdj");
		group.setLq(json.getString("lq"));
		group.setSylx(json.getString("sylx"));
//		group.setSysj(parseDateFlexible(json.getString("pxrq")));
		group.setSize(json.getString("sjcc"));
		group.setSysl(3);
		group.setYxqd(json.getString("qddbz"));
		group.setType(json.getString("type"));
//		String pxlzjz = json.getString("pxlzjz");
//		group.setYxlz(StrUtil.isEmpty(pxlzjz) || pxlzjz.contains("*") || pxlzjz.contains("无效") ? null : json.getFloat("pxlzjz"));
		group.setSybh(json.getString("sybh"));
		group.setSourceId(json.getInteger("autoid"));
		group.setMtype(json.getString("mtype"));
		group.setQddj(json.getString("qddj"));
//		group.set
		group.setCreateDate(new Date());
		ygroupService.save(group);
		//获取试样详细信息
		List<MechanicsYDetailEntity> detailList = new ArrayList<>();
		//每组数据3条子数据；
		for (int i = 1; i < 4; i++) {
			MechanicsYDetailEntity detail = new MechanicsYDetailEntity();
			//拼装压力机试验详情数据
			detail.setGroupId(group.getId());
			//如果是带星*的异常数据，就不插入；
			if (json.getString("kylz" + i).contains("*") || json.getString("kyqd" + i).contains("*")) {
				continue;
			}
			detail.setLoad(json.getFloat("kylz" + i));
			detail.setStress(json.getFloat("kyqd" + i));
			detail.setXs(json.getFloat("xs"));
//			detail.setTestbegin(parseDateFlexible(json.getString("pxrq")));
//			detail.setTestend(parseDateFlexible(json.getString("pxrq")));
			detail.setType(json.getString("type"));
			detail.setYsgcsj(json.getString("qx" + i));
			detailList.add(detail);
		}
		ydetailService.saveBatch(detailList);
		try{
			yGroupDataSend(group.getId());
		} catch (Exception e){
			e.printStackTrace();
		}
		return json.getInteger("autoid");
	}

	private Date parseDateFlexible(String dateStr) {
		if (StrUtil.isBlank(dateStr)) {
			return null;
		}
		List<String> patterns = Arrays.asList(
				"yyyy-MM-dd",
				"yyyy-M-d",
				"yyyy-MM-d",
				"yyyy-M-dd"
		);
		for (String pattern : patterns) {
			try {
				SimpleDateFormat sdf = new SimpleDateFormat(pattern);
				return sdf.parse(dateStr);
			} catch (ParseException e) {
				// 继续尝试下一个格式
			}
		}
		return null;
	}

	private Integer convertTag(String endFlag) {
		if (StrUtil.isEmpty(endFlag)) {
			return null;
		}
		return StrUtil.equals("完成", endFlag) ? 1 : 0;

	}

	private Integer convertResult(String result) {
		if(StrUtil.equals(result,"合格")){
			return 1;
		}
		return null;
	}

	@Autowired
	private MechanicsYGroupService ygroupService;
	@Autowired
	private MechanicsYDetailService ydetailService;
	@Override
	@Transactional
	public Integer saveDataBaseUploadPress(JSONObject json) {
		//拼装压力机试验数据
		MechanicsYGroupEntity group = new MechanicsYGroupEntity();
		group.setPdjg(json.getString("pDJG"));
		group.setShape(json.getString("shape"));
		group.setLq(json.getString("lQ"));
		group.setSylx(json.getString("sYLX"));
		group.setSysj(json.getDate("sYSJ"));
		group.setTester(json.getString("tester"));
		group.setPingzhong(json.getString("pingZhong"));
		group.setSysl(json.getInteger("sYSL"));
		group.setYxqd(json.getString("yXQD"));
		group.setType(json.getString("type"));
		group.setQddj(json.getString("qDDJ"));
		group.setSize(json.getString("size"));
		group.setYxlz(json.getFloat("yXLZ"));
		group.setMachine(json.getString("machine"));
		group.setSybh(json.getString("sYBH"));
		group.setTag(json.getInteger("tag"));
		group.setSourceId(json.getInteger("iD"));
		group.setXs(json.getFloat("xS"));
		group.setMtype(json.getString("mtype"));
		group.setCreateDate(new Date());
		ygroupService.save(group);
		//获取试样详细信息
		JSONArray detailArray= json.getJSONArray("detailList");
		List<MechanicsYDetailEntity> detailList = new ArrayList<MechanicsYDetailEntity>();
		for (Object object : detailArray) {
			JSONObject detailObj = (JSONObject) JSON.toJSON(object);
			MechanicsYDetailEntity detail = new MechanicsYDetailEntity();
			//拼装压力机试验详情数据
			detail.setGroupId(group.getId());
			detail.setLoad(detailObj.getFloat("load"));
			detail.setPingzhong(detailObj.getString("pingZhong"));
			detail.setQddj(detailObj.getString("qDDJ"));
			detail.setShape(detailObj.getInteger("shape"));
			detail.setSize(detailObj.getString("size"));
			detail.setStress(detailObj.getFloat("stress"));
			detail.setTestbegin(detailObj.getDate("testBegin"));
			detail.setTestend(detailObj.getDate("testEnd"));
			detail.setType(detailObj.getString("type"));
			detail.setXh(detailObj.getInteger("xH"));
			detail.setXs(detailObj.getFloat("xS"));
			detail.setYsgcsj(detailObj.getString("ySGCSJ"));
			detailList.add(detail);
		}
		ydetailService.saveBatch(detailList);
		try{
			yGroupDataSend(group.getId());
		} catch (Exception e){
			e.printStackTrace();
		}
		return json.getInteger("iD");
	}

	@Override
	@Transactional
	public Integer saveDataBaseUploadtx2bPress(JSONObject json) {
		//拼装压力机试验数据
		MechanicsYGroupEntity group = new MechanicsYGroupEntity();
		group.setPdjg(json.getString("pDJG"));
		group.setShape(json.getString("shape"));
		group.setLq(json.getString("lQ"));
		group.setSylx(json.getString("sYLX"));
		group.setSysj(json.getDate("sYSJ"));
		group.setTester(json.getString("tester"));
		group.setPingzhong(json.getString("pingZhong"));
		group.setSysl(json.getInteger("sYSL"));
		group.setYxqd(json.getString("yXQD"));
		group.setType(json.getString("type"));
		group.setQddj(json.getString("qDDJ"));
		group.setSize(json.getString("size"));
		group.setYxlz(json.getFloat("yXLZ"));
		group.setMachine(json.getString("machine"));
		group.setSybh(json.getString("sYBH"));
		group.setTag(json.getInteger("tag"));
		group.setSourceId(json.getInteger("iD"));
		group.setXs(json.getFloat("xS"));
		group.setMtype(json.getString("mtype"));
		group.setCreateDate(new Date());
		ygroupService.save(group);
		//获取试样详细信息
		JSONArray detailArray= json.getJSONArray("detailList");
		List<MechanicsYDetailEntity> detailList = new ArrayList<MechanicsYDetailEntity>();
		for (Object object : detailArray) {
			JSONObject detailObj = (JSONObject) JSON.toJSON(object);
			MechanicsYDetailEntity detail = new MechanicsYDetailEntity();
			//拼装压力机试验详情数据
			detail.setGroupId(group.getId());
			detail.setLoad(detailObj.getFloat("load"));
			detail.setPingzhong(detailObj.getString("pingZhong"));
			detail.setQddj(detailObj.getString("qDDJ"));
			detail.setShape(detailObj.getInteger("shape"));
			detail.setSize(detailObj.getString("size"));
			detail.setStress(detailObj.getFloat("stress"));
			detail.setTestbegin(detailObj.getDate("testBegin"));
			detail.setTestend(detailObj.getDate("testEnd"));
			detail.setType(detailObj.getString("type"));
			detail.setXh(detailObj.getInteger("xH"));
			detail.setXs(detailObj.getFloat("xS"));
			detail.setYsgcsj(detailObj.getString("ySGCSJ"));
			detailList.add(detail);
		}
		ydetailService.saveBatch(detailList);
		try{
			yGroupDataSend(group.getId());
		} catch (Exception e){
			e.printStackTrace();
		}
		return json.getInteger("iD");
	}

	//恒田软件，HT.mdb  泰新一标万能机100/300
	@Override
	public Integer saveDataBaseUploadHT(JSONObject json) {
		MechanicsWGroupEntity group = new MechanicsWGroupEntity();
		//拼装万能机试验数据
		//组号
		String zh = json.getString("zH");
		Integer id = -1;
		List<MechanicsWGroupEntity> wGroupEntities = mechanicsWGroupMapper.selectList(new LambdaQueryWrapper<MechanicsWGroupEntity>().eq(MechanicsWGroupEntity::getSybh, zh));
		if (CollUtil.isEmpty(wGroupEntities)) {
			group.setArea(json.getFloat("so"));
//			group.setGczj(json.getFloat("gCZJ"));
			group.setLo(json.getFloat("lo"));
			group.setSylx(json.getString("sYLX"));
			group.setSysj(DATE_NULL.equals(json.getString("kS")) ? null : json.getDate("kS"));
			group.setTester(json.getString("sYRY"));
			group.setSysl(json.getInteger("sL"));
			group.setType(json.getString("type"));
			group.setMachine(json.getString("sYSB"));
			group.setSybh(zh);
			group.setSourceId(json.getInteger("iD"));
//			group.setTag(json.getInteger("tag"));
			group.setCreateDate(new Date());
			groupService.save(group);
			id = group.getId();
            //验证
            try {
                wGroupDataSend(group.getId());
            } catch (Exception e) {
                e.printStackTrace();
            }
		} else {
			id = wGroupEntities.get(0).getId();
		}
		//获取试样详细信息
		MechanicsWDetailEntity detail = new MechanicsWDetailEntity();
		detail.setA(json.getFloat("a"));
		detail.setAgt(json.getFloat("agt"));
		detail.setArea(json.getFloat("so"));
		detail.setAt(json.getFloat("at"));
		detail.setFeh(json.getFloat("feH"));
		detail.setFel(json.getFloat("feL"));
		detail.setFm(json.getFloat("fm"));
		detail.setFp(json.getFloat("fp"));
		detail.setGczj(json.getFloat("gCZJ"));
		detail.setGroupId(id);
		//detail.setGroupId(detailObj.getInteger("gROUP_INDEX_ID"));
		//detail.setId(detailObj.getInteger("iNDEX_ID"));
		detail.setLe(json.getFloat("le"));
		detail.setLo(json.getFloat("lo"));
		detail.setLu(json.getFloat("lu"));
		detail.setReh(json.getFloat("reH"));
		detail.setRel(json.getFloat("reL"));
		detail.setRm(json.getFloat("rm"));
		detail.setRp(json.getFloat("rp"));
		detail.setTestbegin(DATE_NULL.equals(json.getString("kS")) ? null : json.getDate("kS"));
		detail.setTestend(DATE_NULL.equals(json.getString("jS")) ? null : json.getDate("jS"));
		detail.setType(json.getString("type"));
		detail.setXh(json.getInteger("xH"));
		detail.setYsgcsj(getysgcsj(json));
		detail.setDlwz(json.getString("dLWZ"));
		detail.setDlxz(json.getString("ao"));
		wdetailService.save(detail);
		return json.getInteger("iD");
	}

    //恒田软件，HT.mdb  泰新总监办压力机
    @Override
    public Integer saveDataBaseUploadHTY(JSONObject json) {
        MechanicsYGroupEntity group = new MechanicsYGroupEntity();
        //拼装万能机试验数据
        //组号
        String zh = json.getString("zH");
        Integer id = -1;
        List<MechanicsYGroupEntity> yGroupEntities = mechanicsYGroupMapper.selectList(new LambdaQueryWrapper<MechanicsYGroupEntity>().eq(MechanicsYGroupEntity::getSybh, zh));
        if (CollUtil.isEmpty(yGroupEntities)) {
            group.setMj(json.getString("so"));
//			group.setGczj(json.getFloat("gCZJ"));
//            group.setLo(json.getFloat("lo"));
            group.setLq(json.getString("lQ"));
            group.setQddj(json.getString("qDDJ"));
            group.setShape(json.getString("xZ"));
            group.setSpeed(json.getString("sD"));
            group.setSylx(json.getString("sYLX"));
            group.setSysj(DATE_NULL.equals(json.getString("kS")) ? null : json.getDate("kS"));
            group.setTester(json.getString("sYRY"));
            group.setYxlz(json.getFloat("yXLZ"));
            group.setYxqd(json.getString("yXQD"));
            group.setSysl(json.getInteger("sL"));
            group.setType(json.getString("type"));
            group.setMtype(json.getString("mType"));
            group.setMachine(json.getString("sYSB"));
            group.setSybh(zh);
            group.setSourceId(json.getInteger("iD"));
//			group.setTag(json.getInteger("tag"));
            group.setCreateDate(new Date());
            ygroupService.save(group);
            id = group.getId();
            //验证
            try {
                wGroupDataSend(group.getId());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            id = yGroupEntities.get(0).getId();
        }
        //获取试样详细信息
        MechanicsYDetailEntity detail = new MechanicsYDetailEntity();
//        detail.setA(json.getFloat("a"));
//        detail.setAgt(json.getFloat("agt"));
//        detail.setArea(json.getFloat("so"));
//        detail.setAt(json.getFloat("at"));
//        detail.setFeh(json.getFloat("feH"));
//        detail.setFel(json.getFloat("feL"));
//        detail.setFm(json.getFloat("fm"));
//        detail.setFp(json.getFloat("fp"));
//        detail.setGczj(json.getFloat("gCZJ"));
        detail.setGroupId(id);
        //detail.setGroupId(detailObj.getInteger("gROUP_INDEX_ID"));
        //detail.setId(detailObj.getInteger("iNDEX_ID"));
        detail.setLoad(json.getFloat("yXLZ"));
        detail.setQddj(json.getString("qDDJ"));
        detail.setStress(json.getFloat("yXQD"));
//        detail.setLo(json.getFloat("lo"));
//        detail.setLu(json.getFloat("lu"));
//        detail.setReh(json.getFloat("reH"));
//        detail.setRel(json.getFloat("reL"));
//        detail.setRm(json.getFloat("rm"));
//        detail.setRp(json.getFloat("rp"));
        detail.setTestbegin(DATE_NULL.equals(json.getString("kS")) ? null : json.getDate("kS"));
        detail.setTestend(DATE_NULL.equals(json.getString("jS")) ? null : json.getDate("jS"));
        detail.setType(json.getString("type"));
        detail.setXh(json.getInteger("xH"));
        detail.setYsgcsj(getysgcsjy(json));
//        detail.setDlwz(json.getString("dLWZ"));
//        detail.setDlxz(json.getString("ao"));
        ydetailService.save(detail);
        return json.getInteger("iD");
    }

	//组装曲线数据,万能机
	private String getysgcsj(JSONObject json) {
		String time = json.getString("tIME");
		String load = json.getString("lOAD");
		String posi = json.getString("pOSI");
		String extn = json.getString("eXTN");
		if (StrUtil.isEmpty(time) || StrUtil.isEmpty(load) || StrUtil.isEmpty(posi) || StrUtil.isEmpty(extn)) {
			return null;
		}
		// 分割各个字段为数组
		String[] array1 = time.split(",", -1);
		String[] array2 = load.split(",", -1);
		String[] array3 = posi.split(",", -1);
		String[] array4 = extn.split(",", -1);

		// 确保数组长度一致（基于第一个字段）
		int length = array1.length;
		// 如果其他字段长度不一致，需要处理
		if (array2.length != length || array3.length != length || array4.length != length) {
			// 取最大长度进行处理
			length = Math.max(Math.max(Math.max(array1.length, array2.length),
					array3.length), array4.length);
		}
		StringBuilder result = new StringBuilder();
		// 轮流取值并拼接
		for (int i = 0; i < length; i++) {
			if (i > 0) {
				result.append(";");
			}
			String value1 = i < array1.length ? array1[i] : "";
			String value2 = i < array2.length ? array2[i] : "";
			String value3 = i < array3.length ? array3[i] : "";
			String value4 = i < array4.length ? array4[i] : "";
			result.append(value1).append(",")
					.append(value2).append(",")
					.append(value3).append(",")
					.append(value4);
		}

		return result.toString();
	}

    //组装曲线数据，压力机
    private String getysgcsjy(JSONObject json) {
        String time = json.getString("tIME");
        String load = json.getString("lOAD");
        String posi = json.getString("pOSI");
        String extn = json.getString("eXTN");
        if (StrUtil.isEmpty(time) || StrUtil.isEmpty(load)) {
            return null;
        }
        // 分割各个字段为数组
        String[] array1 = time.split(",", -1);
        String[] array2 = load.split(",", -1);
        String[] array3 = posi.split(",", -1);
        String[] array4 = extn.split(",", -1);

        // 确保数组长度一致（基于第一个字段）
        int length = array1.length;
        // 如果其他字段长度不一致，需要处理
        if (array2.length != length || array3.length != length || array4.length != length) {
            // 取最大长度进行处理
            length = Math.max(Math.max(Math.max(array1.length, array2.length),
                array3.length), array4.length);
        }
        StringBuilder result = new StringBuilder();
        // 轮流取值并拼接
        for (int i = 0; i < length; i++) {
            if (i > 0) {
                result.append(";");
            }
            String value1 = i < array1.length ? array1[i] : "";
            String value2 = i < array2.length ? array2[i] : "";
            String value3 = i < array3.length ? array3[i] : "";
            String value4 = i < array4.length ? array4[i] : "";
            result.append(value1).append(",")
                .append(value2).append(",")
                .append(value3).append(",")
                .append(value4);
        }

        return result.toString();
    }

	@Override
	public Integer saveDataBaseUploadOUKPress(JSONObject json) {
		//拼装压力机试验数据
		MechanicsYGroupEntity group = new MechanicsYGroupEntity();
		//简单的评定结果，联网厂家的评定方式与我们不同的话请自行处理
		group.setPdjg("");
		//截面形状，0表示圆形，1表示矩形，2表示方形，3表示不规则，4表示无法识别
		group.setShape("4");
		//龄期 针对有龄期的试验如水泥,其它试验忽略
		group.setLq(json.getString("lq"));
		//试验类型，用于识别相应试验
		group.setSylx(json.getString("synx"));
		//实验时间
		group.setSysj(json.getDate("date_time"));
		//试验员
		group.setTester("");

		//group.setPingzhong(json.getString("pingZhong"));

		//本组的试样数量
		group.setSysl(json.getInteger("numb"));
		//有效强度
		group.setYxqd(json.getString("yxqd"));
		//数据来源
		group.setType(json.getString("zDBH"));
		//强度等级
		group.setQddj(json.getString("qddj"));
		//根据截面形状不同，表示的含义不同。圆形为直径或者直径高度；矩形为宽厚或者宽厚高，方
		group.setSize(json.getString("sjgg"));
		//有效力值
		group.setYxlz(json.getFloat("yxlz"));
		//设备编号
		group.setMachine("");
		//试验编号、欧凯填入外部编号数值
		group.setSybh(json.getString("wbbh"));
		//状态标记 0:该组试验未完成 1:该组试验完成
		group.setTag(1);
		//数据抽取源表主键
		group.setSourceId(json.getInteger("iD"));
		//系数,有些试验在不是标准规格的时候，可能需要转换系数
		try{
			//欧凯使用的是修正系数字段
			group.setXs(json.getFloat("xzxs"));
		}catch (Exception e){
			group.setXsok(json.getString("xzxs"));
		}
		//创建时间
		group.setCreateDate(new Date());
		//加荷速度
		group.setSpeed(json.getString("speed"));
		//跨距
		group.setKj(json.getString("kj"));
		//面积
		group.setMj(json.getString("mj"));
		//欧凯数据文件（曲线数据在这个指定的文件内）
		group.setSjwj(json.getString("sjwj"));
		group.setMtype(json.getString("mType"));
		ygroupService.save(group);
		//获取试样详细信息
		List<MechanicsYDetailEntity> detailList = new ArrayList<MechanicsYDetailEntity>();
		int numb = json.getInteger("numb");
		for (int i = 1; i <= numb; i++) {
			String zhz = json.getString("zhz"+i);
			String qdz = json.getString("qdz"+i);
			MechanicsYDetailEntity detail = new MechanicsYDetailEntity();
			//拼装压力机试验详情数据
			detail.setGroupId(group.getId());
			//极限荷载(kN）
			detail.setLoad(Float.parseFloat(zhz));
			//极限强度(Mpa)
			detail.setStress(Float.parseFloat(qdz));
			detail.setPingzhong(json.getString("sjpz"));
			detail.setQddj(json.getString("qddj"));
			//截面形状，0表示圆形，1表示矩形，2表示方形，3表示不规则。
			detail.setShape(4);
			//根据截面形状不同，表示的含义不同。圆形为直径或者直径高度；矩形为宽厚或者宽厚高，方形
			detail.setSize(json.getString("sjgg"));
//			detail.setTestbegin(json.getDate("date_time"));
//			detail.setTestend(json.getDate("date_time"));
			//终端编号
			detail.setType(json.getString("zDBH"));
			//序号 代表第几块
			detail.setXh(i);
			try{
				detail.setXs(json.getFloat("xzxs"));
			}catch (Exception e){

			}
			detailList.add(detail);
		}
		ydetailService.saveBatch(detailList);

		try{
			yGroupDataSend(group.getId());
		} catch (Exception e){
			e.printStackTrace();
		}
		return json.getInteger("iD");
	}
	@Autowired
	private MechanicsWFyDetailService wfyService;
	@Override
	public Integer saveDataBaseUploadFYAllPurpose(JSONObject json) {
		MechanicsWGroupEntity group = new MechanicsWGroupEntity();
		//拼装万能机试验数据
//		group.setArea(json.getFloat("area"));
//		group.setGczj(json.getFloat("gCZJ"));
//		group.setLo(json.getFloat("lo"));
		group.setSylx(json.getString("sYLX"));
		group.setSysj(json.getDate("sYSJ"));
//		group.setTester(json.getString("tester"));
		group.setSysl(json.getInteger("sYSL"));
		group.setType(json.getString("zDBH"));
//		group.setMachine(json.getString("machine"));
		group.setSybh(json.getString("sYBH"));
		group.setSourceId(json.getInteger("iD"));

		if("已完成".equals(json.getString("tag"))){
			group.setTag(1);
		}else{
			group.setTag(0);
		}
		group.setCreateDate(new Date());
		group.setSourceType("1");
		//判断该组数据简介是否进行维护
		QueryWrapper query = new QueryWrapper();
		query.eq("SYBH",group.getSybh());
		query.eq("type",group.getType());
		MechanicsWGroupEntity groupDate =groupService.getOne(query);
		if(groupDate==null){
			groupService.save(group);
		}
		//详情数据维护
		MechanicsWFyDetailEntity detail = new MechanicsWFyDetailEntity();
		if(groupDate==null){
			detail.setGroupId(group.getId());
		}else{
			detail.setGroupId(groupDate.getId());
		}
		detail.setSybh(json.getString("sYBH"));
		detail.setLq(json.getString("lQ"));
		detail.setXh(json.getString("xH"));
		detail.setSysl(json.getString("sYSL"));
		detail.setTag(json.getString("tag"));
		detail.setSylx(json.getString("sYLX"));
		detail.setSysj(json.getDate("sYSJ"));
		detail.setCxsj(json.getString("cXSJ"));
		detail.setZdsl(json.getString("zDSL"));
		detail.setSblc(json.getString("sBLC"));
		detail.setLzdw(json.getString("lZDW"));
		detail.setZdl(json.getString("zDL"));
		detail.setQfd(json.getString("qFD"));
		detail.setRp02(json.getString("rp02"));
		detail.setYsgcsj(json.getString("ySGCSJ"));
		detail.setType(json.getString("zDBH"));
		wfyService.save(detail);
		try{
			wGroupDataSend(group.getId());
		} catch (Exception e){
			e.printStackTrace();
		}
		return json.getInteger("iD");
	}
	@Override
	public Integer saveDataBaseUploadOKAllPurpose(JSONObject json) {
		MechanicsWGroupEntity group = new MechanicsWGroupEntity();
		//拼装万能机试验数据
		group.setArea(json.getFloat("area"));
		group.setGczj(json.getFloat("gCZJ"));
		group.setLo(json.getFloat("lo"));
		group.setSylx(json.getString("sYLX"));
		group.setSysj(json.getDate("sYSJ"));
		group.setTester(json.getString("tester"));
		group.setSysl(json.getInteger("sYSL"));
		group.setType(json.getString("zDBH"));
		group.setMachine(json.getString("machine"));
		group.setSybh(json.getString("sYBH"));
		group.setSourceId(json.getInteger("iD"));

		if("已完成".equals(json.getString("tag"))){
			group.setTag(1);
		}else{
			group.setTag(0);
		}
		group.setCreateDate(new Date());
		group.setSourceType("1");
		//判断该组数据简介是否进行维护
		QueryWrapper query = new QueryWrapper();
		query.eq("SYBH",group.getSybh());
		query.eq("type",group.getType());
		MechanicsWGroupEntity groupDate =groupService.getOne(query);
		if(groupDate==null){
			groupService.save(group);
		}
		//详情数据维护
		MechanicsWFyDetailEntity detail = new MechanicsWFyDetailEntity();
		if(groupDate==null){
			detail.setGroupId(group.getId());
		}else{
			detail.setGroupId(groupDate.getId());
		}
		detail.setSybh(json.getString("sYBH"));
		detail.setLq(json.getString("lQ"));
		detail.setXh(json.getString("xH"));
		detail.setSysl(json.getString("sYSL"));
		detail.setTag(json.getString("tag"));
		detail.setSylx(json.getString("sYLX"));
		detail.setSysj(json.getDate("sYSJ"));
		detail.setCxsj(json.getString("cXSJ"));
		detail.setZdsl(json.getString("zDSL"));
		detail.setSblc(json.getString("sBLC"));
		detail.setLzdw(json.getString("lZDW"));
		detail.setZdl(json.getString("zDL"));
		detail.setQfd(json.getString("qFD"));
		detail.setRp02(json.getString("rp02"));
		detail.setYsgcsj(json.getString("ySGCSJ"));
		detail.setType(json.getString("zDBH"));
		wfyService.save(detail);
		try{
			wGroupDataSend(group.getId());
		} catch (Exception e){
			e.printStackTrace();
		}
		return json.getInteger("iD");
	}
	@Override
	public Integer saveDataBaseUploadFYYAllPurpose(JSONObject json) {
		MechanicsYGroupEntity group = new MechanicsYGroupEntity();
		//试验类型
		group.setSylx(json.getString("sYLX"));
		//试验编号
		group.setSybh(json.getString("sYBH"));
		//试样数量
		group.setSysl(json.getInteger("sYSL"));
		//龄期
		group.setLq(json.getString("lQ"));
		//形状(无)

		//尺寸
		group.setSize(json.getString("sIZE"));
		//系数(无)

		//强度等级
		group.setQddj(json.getString("qDDJ"));
		//品种(无)

		//有效力值
		group.setYxlz(json.getFloat("zDL"));
		//有效强度
		group.setYxqd(json.getString("qFD"));
		//试验时间
		group.setSysj(json.getDate("sYSJ"));
		group.setType(json.getString("zDBH"));
		group.setSourceId(json.getInteger("iD"));
		if("已完成".equals(json.getString("tag"))){
			group.setTag(1);
		}else{
			group.setTag(0);
		}
		group.setCreateDate(new Date());
		//判断该组数据是否需要进行维护
		QueryWrapper query = new QueryWrapper();
		query.eq("SYBH",group.getSybh());
		query.eq("type",group.getType());
		MechanicsYGroupEntity groupDate =ygroupService.getOne(query);
		if(groupDate==null){
			ygroupService.save(group);

		}
		//详情数据维护
		MechanicsYDetailEntity detail = new MechanicsYDetailEntity();
		if(groupDate==null){
			detail.setGroupId(group.getId());
		}else{
			detail.setGroupId(groupDate.getId());
		}
		//拼装压力机试验详情数据
		detail.setXh(json.getInteger("xH"));
		detail.setSize(json.getString("sIZE"));
		detail.setQddj(json.getString("qDDJ"));
		detail.setLoad(json.getFloat("zDL"));
		detail.setStress(json.getFloat("qFD"));
		detail.setTestbegin(json.getDate("sYSJ"));
		detail.setTestend(json.getDate("cXSJ"));
		detail.setType(json.getString("zDBH"));
		detail.setYsgcsj(json.getString("ySGCSJ"));
		ydetailService.save(detail);
		//当试验数量等于序号时
		if(group.getSysl()==detail.getXh()){
			try{
				//传参主键ID
				yGroupDataSend(detail.getGroupId());
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		return json.getInteger("iD");
	}

	/**
	 * 软化点数据存储
	 * @param json
	 * @return
	 */
	@Override
	public Integer saveDataBaseUploadZrd(JSONObject json) {
		AsphaltPenetrometerResEntity entity = new AsphaltPenetrometerResEntity();
		//"zRDValue3" -> {Double@13486} 2.5
		entity.setZrdValue3(json.getBigDecimal("zRDValue3"));
		//"zRDValue2" -> {Double@13488} 6.4
		entity.setZrdValue2(json.getBigDecimal("zRDValue2"));
		//"zRDValue1" -> {Double@13490} 5.2
		entity.setZrdValue1(json.getBigDecimal("zRDValue1"));
		//"sampleNo" -> "test1"
		entity.setSampleNo(json.getString("sampleNo"));
		//"standardValue2" -> {Double@13492} 45.6
		entity.setStandardValue2(json.getBigDecimal("standardValue2"));
		//"type" -> "test"
		entity.setType(json.getString("type"));
		//"standardValue1" -> {Double@13496} 83.4
		entity.setStandardValue1(json.getBigDecimal("standardValue1"));
		//"sampleMS" -> "test1"
		entity.setSampleMS(json.getString("sampleMS"));
		//"sampleName" -> "test1"
		entity.setSampleName(json.getString("sampleName"));
		//"zRDAvg" -> {Double@13501} 2.5
		entity.setZrdAvg(json.getBigDecimal("zRDAvg"));
		//"type" -> "test"
		entity.setDeviceId(json.getString("type"));
		// 数据类型：1 Access数据库获取
		entity.setDataSource("1");
		//"sYJID" -> "TEST001"
		entity.setGroupNumber(json.getString("groupNumber"));
		//"gCBuWei" -> "test1"
		entity.setGCBuWei(json.getString("gCBuWei"));
		//上传时间
		//获取当前时间
		entity.setCreateTime(new Date());
		//"testerName" -> "test1002"
		entity.setTesterName(json.getString("testerName"));
		//"sequence" -> {Integer@13513} 1
		entity.setSequence(json.getLong("sequence"));
		//"submitTime" -> "2024-06-25"
		entity.setSubmitTime(json.getDate("iS_TESTTIME"));
		//"zRD2Process" -> "阿萨"
		entity.setZrd2Process(json.getString("zRD2Process"));
		//"zRD1Process" -> "靠靠靠艰苦艰苦"
		entity.setZrd1Process(json.getString("zRD1Process"));
		//"zRD3Process" -> "无穷大"
		entity.setZrd3Process(json.getString("zRD3Process"));
		//"projectName" -> "test1"
		entity.setProjectName(json.getString("projectName"));
		//"testCondition" -> "test1"
		entity.setTestCondition(json.getString("testCondition"));
		// 针入度数据保存
		asphaltPenetrometerResService.saveData(entity);
		return json.getInteger("sequence");
	}

	@Override
	public Integer saveDataBaseUploadSwBhzSb(JSONObject json) {
		NCementJwDataEntity entity = new NCementJwDataEntity();
		//source_id
		entity.setSourceId(json.getInteger("bianhao"));
		//occur_time
		entity.setOccurTime(json.getDate("shijian"));
		//骨料总量
		entity.setGlzl(json.getString("glchangliang"));
		//sjgl1
		entity.setSjgl1(json.getString("sjgl1"));
		//sjgl2
		entity.setSjgl2(json.getString("sjgl2"));
		//sjgl3
		entity.setSjgl3(json.getString("sjgl3"));
		//sjgl4
		entity.setSjgl4(json.getString("sjgl4"));
		//sjgl5
		entity.setSjgl5(json.getString("sjgl5"));
		//actual_water
		entity.setActualWater(json.getString("sjshui"));
		//sjfl1
		entity.setSjfl1(json.getString("sjfl1"));
		//sjfl2
		entity.setSjfl2(json.getString("sjfl2"));
		//llgl1
		entity.setLlgl1(json.getString("llgl1"));
		//llgl2
		entity.setLlgl2(json.getString("llgl2"));
		//llgl3
		entity.setLlgl3(json.getString("llgl3"));
		//llgl4
		entity.setLlgl4(json.getString("llgl4"));
		//llgl5
		entity.setLlgl5(json.getString("llgl5"));
		//theoretical_water
		entity.setTheoreticalWater(json.getString("llshui"));
		//llfl1
		entity.setLlfl1(json.getString("llfl1"));
		//llfl2
		entity.setLlfl2(json.getString("llfl2"));
		//create_date
		entity.setCreateDate(new Date());
		//type
		entity.setType(json.getString("type"));
		nCementJwDataService.saveData(entity);
		return json.getInteger("bianhao");
	}

	@Override
	public Integer saveDataBaseUploadSwBhzSbNew(JSONObject json) {
		NCementJwDataEntity entity = new NCementJwDataEntity();
		//source_id
		entity.setSourceId(json.getInteger("bianhao"));
		//occur_time
		entity.setOccurTime(json.getDate("shijian"));
		//骨料总量
		entity.setGlzl(json.getString("glchangliang"));
		//sjgl1
		entity.setSjgl1(json.getString("sjgl1"));
		//sjgl2
		entity.setSjgl2(json.getString("sjgl2"));
		//sjgl3
		entity.setSjgl3(json.getString("sjgl3"));
		//sjgl4
		entity.setSjgl4(json.getString("sjgl4"));
		//sjgl5
		entity.setSjgl5(json.getString("sjgl5"));
		//actual_water
		entity.setActualWater(json.getString("sjshui"));
		//sjfl1
		entity.setSjfl1(json.getString("sjfl1"));
		//sjfl2
		entity.setSjfl2(json.getString("sjfl2"));
		//llgl1
		entity.setLlgl1(json.getString("llgl1"));
		//llgl2
		entity.setLlgl2(json.getString("llgl2"));
		//llgl3
		entity.setLlgl3(json.getString("llgl3"));
		//llgl4
		entity.setLlgl4(json.getString("llgl4"));
		//llgl5
		entity.setLlgl5(json.getString("llgl5"));
		//theoretical_water
		entity.setTheoreticalWater(json.getString("llshui"));
		//llfl1
		entity.setLlfl1(json.getString("llfl1"));
		//llfl2
		entity.setLlfl2(json.getString("llfl2"));
		//create_date
		entity.setCreateDate(new Date());
		//type
		entity.setType(json.getString("type"));
		entity.setPeifangName(json.getString("peifangName"));
		nCementJwDataService.saveData(entity);
		return json.getInteger("bianhao");
	}

	//四标
	@Override
	public Integer saveDataBaseUploadSwBhzFb(JSONObject json) {
		NCementJwDataEntity entity = new NCementJwDataEntity();
		//source_id
		entity.setSourceId(json.getInteger("bianhao"));
		//occur_time
		entity.setOccurTime(json.getDate("kssj"));
		//end_time
		entity.setEndTime(json.getDate("jssj"));
		//骨料总量
//		entity.setGlzl(unitConversion(json.getString("glchangliang")));
		entity.setGlzl(json.getString("glchangliang"));
		//sjgl1
//		entity.setSjgl1(unitConversion(json.getString("sjgl1")));
		entity.setSjgl1(json.getString("sjgl1"));
		//sjgl2
//		entity.setSjgl2(unitConversion(json.getString("sjgl2")));
		entity.setSjgl2(json.getString("sjgl2"));
		//sjgl3
//		entity.setSjgl3(unitConversion(json.getString("sjgl3")));
		entity.setSjgl3(json.getString("sjgl3"));
		//sjgl4
//		entity.setSjgl4(unitConversion(json.getString("sjgl4")));
		entity.setSjgl4(json.getString("sjgl4"));
		//sjgl5
//		entity.setSjgl5(unitConversion(json.getString("sjgl5")));
		entity.setSjgl5(json.getString("sjgl5"));
		//actual_water
//		entity.setActualWater(unitConversion(json.getString("sjshui")));
		entity.setActualWater(json.getString("sjshui"));
		//sjfl1
		entity.setSjfl1(json.getString("sjfl1"));
		//sjfl2
		entity.setSjfl2(json.getString("sjfl2"));
		//llgl1
		entity.setLlgl1(json.getString("llgl1"));
		//llgl2
		entity.setLlgl2(json.getString("llgl2"));
		//llgl3
		entity.setLlgl3(json.getString("llgl3"));
		//llgl4
		entity.setLlgl4(json.getString("llgl4"));
		//llgl5
		entity.setLlgl5(json.getString("llgl5"));
		//theoretical_water
		entity.setTheoreticalWater(json.getString("llshui"));
		//llfl1
		entity.setLlfl1(json.getString("llfl1"));
		//llfl2
		entity.setLlfl2(json.getString("llfl2"));
		//create_date
		entity.setCreateDate(new Date());
		//type
		entity.setType(json.getString("type"));
		//配方名
		if (json.getString("recipe") != null){
			entity.setRecipe(json.getString("recipe"));
		}
		nCementJwDataService.saveData(entity);
		return json.getInteger("bianhao");
	}

	//一标
	@Override
	public Integer saveDataBaseUploadSwBhzOb(JSONObject json) {
		NCementJwDataEntity entity = new NCementJwDataEntity();
		//source_id
		entity.setSourceId(json.getInteger("bianhao"));
		//occur_time
		entity.setOccurTime(json.getDate("kssj"));
		//end_time
		entity.setEndTime(json.getDate("jssj"));
		//骨料总量
		entity.setGlzl(json.getString("glchangliang"));
		//sjgl1
		entity.setSjgl1(json.getString("sjgl1"));
		//sjgl2
		entity.setSjgl2(json.getString("sjgl2"));
		//sjgl3
		entity.setSjgl3(json.getString("sjgl3"));
		//sjgl4
		entity.setSjgl4(json.getString("sjgl4"));
		//sjgl5
		entity.setSjgl5(json.getString("sjgl5"));
		//actual_water
		entity.setActualWater(json.getString("sjshui"));
		//sjfl1
		entity.setSjfl1(json.getString("sjfl1"));
		//sjfl2
		entity.setSjfl2(json.getString("sjfl2"));
		//llgl1
		entity.setLlgl1(json.getString("llgl1"));
		//llgl2
		entity.setLlgl2(json.getString("llgl2"));
		//llgl3
		entity.setLlgl3(json.getString("llgl3"));
		//llgl4
		entity.setLlgl4(json.getString("llgl4"));
		//llgl5
		entity.setLlgl5(json.getString("llgl5"));
		//theoretical_water
		entity.setTheoreticalWater(json.getString("llshui"));
		//llfl1
		entity.setLlfl1(json.getString("llfl1"));
		//llfl2
		entity.setLlfl2(json.getString("llfl2"));
		//create_date
		entity.setCreateDate(new Date());
		//type
		entity.setType(json.getString("type"));
		//配方名
		if (json.getString("recipe") != null){
			entity.setRecipe(json.getString("recipe"));
		}
		nCementJwDataService.saveData(entity);
		return json.getInteger("bianhao");
	}
	/**
	 * 单位转换方法，将可转换为数字类型的数据乘以1000后返回
	 * @return
	 */
	private String unitConversion(String data){
		// 判断data是否可转换数字类型(BigDecimal)
		if(StringUtils.isNotBlank(data)){
			try {
				BigDecimal bd = new BigDecimal(data);
				return bd.multiply(new BigDecimal(1000)).toString();
			} catch (Exception e) {
				return data;
			}
		}else{
			return data;
		}

	}

	@Override
	public void sendTest(Map<String, String> params) {
		String id =params.get("id");
		try {
			wGroupDataSend(Integer.parseInt(id));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		System.out.println("ok");
	}



	//校验合格
	private void yGroupDataSend(Integer id) throws IOException {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet("http://111.14.222.59:9163/prod-api/zhgd/laboratoryMechanicsYGroup/verifyEligible?id="+id);
		CloseableHttpResponse response = httpClient.execute(httpGet);
		int status = response.getStatusLine().getStatusCode();
		if (status >= 200 && status < 300) {
			// 对得到后的实例可以进行处理，例如读取回复体，读取html
			HttpEntity entity = response.getEntity();
			System.out.println(new Date());
			System.out.println("=======================");
			String html = EntityUtils.toString(entity);
			System.out.println(html);
		} else {
			throw new ClientProtocolException("Unexpected response status: " + status);
		}
		// 6. 释放连接
		response.close();
		httpClient.close();
	}
	private void wGroupDataSend(Integer id) throws IOException {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet("http://111.14.222.59:9163/prod-api/zhgd/laboratoryMechanicsWGroup/verifyEligible?id="+id);
		CloseableHttpResponse response = httpClient.execute(httpGet);
		int status = response.getStatusLine().getStatusCode();
		if (status >= 200 && status < 300) {
			// 对得到后的实例可以进行处理，例如读取回复体，读取html
			HttpEntity entity = response.getEntity();
			System.out.println(new Date());
			System.out.println("=======================");
			String html = EntityUtils.toString(entity);
			System.out.println(html);
		} else {
			throw new ClientProtocolException("Unexpected response status: " + status);
		}
		// 6. 释放连接
		response.close();
		httpClient.close();
	}
}
